from selenium import webdriver # 驱动浏览器
from selenium.webdriver.common.by import By # 定位
from selenium.webdriver import ActionChains # 鼠标悬停的包
import time

# 打开浏览器
d = webdriver.Chrome() # 打开谷歌浏览器
d.maximize_window() # 最大化窗口
# 打开网址
d.get("http://192.168.0.125:9009/")
# 滚动条 下拉
js = "window.scrollTo(0,1000);"
d.execute_script(js) # 执行 js 语句
# 通过 文本链接 定位到  登录按钮
login_button = d.find_element(By.LINK_TEXT,'登录')
# 点击登录按钮
login_button.click()

# 通过 CSS选择器 定位到 用户名输入框
username_ele = d.find_element(By.CSS_SELECTOR,'[name="log"]')
# 通过 CSS选择器 定位到 密码输入框
password_ele = d.find_element(By.CSS_SELECTOR,'[id="user_pass"]')
# 通过 CSS选择器 定位到 登录按钮
login_ele = d.find_element(By.CSS_SELECTOR,'[name="wp-submit"]')

# 输入用户名与密码
username_ele.send_keys('admin')
time.sleep(1)
password_ele.send_keys('admin123')
time.sleep(0.5)
login_ele.click()

# 定位到 需要 悬停的元素
post_link = d.find_element(By.CSS_SELECTOR,'[id="menu-posts"] [class="wp-menu-name"]')
# ActionChains(浏览器).move_to_element(移动到哪个元素).perform()
ActionChains(d).move_to_element(post_link).perform()
time.sleep(1)

write_ele = d.find_element(By.LINK_TEXT,'写文章')
write_ele.click()

# 输入标题
write_title = d.find_element(By.CSS_SELECTOR,'[name="post_title"]')
write_title.send_keys('哈哈哈哈哈')
time.sleep(1)

# 切换表单
# 1、先定位到表单所处的 frame 或者  iframe 标签
frame_ele = d.find_element(By.CSS_SELECTOR,'[id="content_ifr"]')
# 2、切换
d.switch_to.frame( frame_ele )
time.sleep(1)

# 输入正文
content_ele = d.find_element(By.CSS_SELECTOR,'[id="tinymce"]')
content_ele.send_keys('输入正文啊啊啊啊啊啊')
time.sleep(1)

# 跳出表单
d.switch_to.default_content()

# 点击发布按钮
push_ele = d.find_element(By.CSS_SELECTOR,'[name="publish"]')
push_ele.click()
time.sleep(10)
d.quit()

